// packageK/components/chart/chart.js
import * as echarts from '../ec-canvas/echarts';
let chart = {}
function initChart(canvas, width, height, dpr, id) {
  chart[id] = echarts.init(canvas, null, {
    width: width,
    height: height,
    devicePixelRatio: dpr // new
  });
  canvas.setChart(chart[id]);
  return chart[id];
}
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    chartId: {
      type: String,
      value: ""
    },
    canvasId: {
      type: String,
      value:""
    },
    options: {
      type: Object,
      value: {}
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    ec: {
      onInit: initChart
    },
    oldCanvas: false
  },

  /**
   * 数据监听
   */
  observers: {
    options: function(o) {
      if (chart[this.data.canvasId]) {
        chart[this.data.canvasId].setOption(o)
      }
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    
  },

  /**
   * 生命周期
   */
  lifetimes: {
    ready: function() {
      // 解决echarts在模拟器和真机层级问题
      const _this = this
      wx.getSystemInfo({
        success: function(res) {
          _this.setData({
            oldCanvas: res.platform == 'devtools'
          })
        }
      })
    }
  }

})